<?php
App::uses('AppController', 'Controller');

class ProductsController extends AppController {

	public $helpers = array('Captcha');
	public $components = array(
		//'Security',
		'Captcha',
		'Session',
		'Paginator',
		'Upload',
	);
	public function index(){
		$changeBgTr = 'onMouseOver="this.style.background=\''. ConstAdminComponent::BACKGROUND_MOUSEOVER .'\'" onMouseOut="this.style.background=\''. ConstAdminComponent::BACKGROUND_MOUSEOUT .'\'"';

		//get param filter
		$conditions = array('Product.pro_active' => 1);
		if ($this->request->query) {
			$pro_name = $this->request->query['pro_name'];
			if(!empty($pro_name)){
				$conditions['Product.pro_name'] = 'like %'.$pro_name.'%';
			}

			$pro_category = $this->request->query['pro_category'];
			if(!empty($pro_category)){
				$conditions['Product.pro_category'] = $pro_category;
			}
		}

		$paginate = array(
			//'fields' => array('pro_id', 'pro_name', 'pro_teaser'),
			'limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
			'conditions' => $conditions,
		);

		$this->Paginator->settings = $paginate;
		$datas = $this->Paginator->paginate('Product');

		$this->set(array(
				'datas' => $datas,
				'changeBgTr' => $changeBgTr,
		));

		$this->set('page_number_limit', ConstAdminComponent::PAGE_NUMBER_LIMIT);
		$this->set('title_for_layout',__('Products'));

	}

	/**
	 * Create news product
	 */
	public function createProduct(){
		if ($this->request->is('post')) {
			//if($this->Captcha->validateCaptcha()) {
			$this->Product->create();
			$params = $this->request->data;
			if ($params['Product']['file']){
				$uploadImage = $this->Upload->uploadImage(reset($params['Product']['file']), FOLDER_UPLOAD_IMAGE);
				if ($this->Upload->error == ''){
					$params['Product']['pro_picture'] = $this->Upload->filename;
					unset($params['Product']['file']);
				} else {
					$this->Session->setFlash(__($uploadImage['error']), 'error');
					$this->set('params', $params);
					$this->redirect(array('action' => 'createProduct'));
				}
			}

			if ($this->Product->save($params)) {
				$this->Session->setFlash(__('Success'),'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Save could not be saved. Please, try again'), 'error');

				//$this->redirect(array('action' => 'createProduct'));
			}
			//$this->set('params', $params);
			// 			} else {
			// 				$this->Session->setFlash(__('Captcha-Error. Please, try again'), 'message');
			// 			}
		}

		$this->setView('create');
	}
	/**
	 * Edit product
	 * @param int $id
	 *
	 */
	public function edit($id = null){
		$data = $this->Product->getProductDetail($id);
		if ($this->request->is('post')) {
			$updateProducts = array();

			$fileImage = $this->request['data']['products']['file'];
			if ($fileImage[0]['error'] == 0){
				$uploadImage = $this->Upload->uploadImage($fileImage[0], FOLDER_UPLOAD_IMAGE);
				if ($uploadImage['error'] == ''){
					$updateProducts['pro_picture'] = "'" . $uploadImage['file_name'] . "'";
				} else {
					$this->Session->setFlash(__($uploadImage['error']), 'error');
					$this->redirect(array('action' => 'edit', 'id' => $id));
				}
			}

			$updateProducts['pro_name'] = "'" . $this->request['data']['products']['pro_name'] . "'";
			$updateProducts['pro_active'] = $this->request['data']['products']['pro_active'];
			$updateProducts['pro_price'] = $this->request['data']['products']['pro_price'];
			$updateProducts['pro_new'] = $this->request['data']['products']['pro_new'];
			$updateProducts['pro_teaser'] = "'" . $this->request['data']['products']['pro_teaser'] . "'";

			$conditionProduct = array(
					'pro_id' => $id,
			);
			$this->Product->updateAll($updateProducts, $conditionProduct);
			$this->redirect(array("controller" => "Products",
					"action" => "index"));
		}

		$this->set('datas', $data);
	}

	/**
	 *
	 * @param string $id
	 */
	public function delete($id = null){
		$this->layout = null;
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}

		$this->Product->read('id', $id);
		$this->Product->set('pro_active', 0);
		$this->Product->save();
		$this->Session->setFlash(__('Delete success'),'success');
		return $this->redirect(array('action' => 'index'));
	}

	public function captcha()
	{
		$this->Captcha->configCaptcha(
			array(
				'width'=>200,//image width -default 200
				'height'=>70,//image height-default 70
				'quality'=>2,//set quality-high:3 ,mid:2 ,low:1
				'blur'=>false,//enablle/disable blur
				'format'=>'jpeg',//iage formats. jpeg or png
				'wordFile'=>false,//include word file or not
				'maxLength'=>5,//min. word length
				'minLength'=>8,//max word length
				//'background'=>array(87,87,87), //rgb color code here
				'color'=>array(
						array(128,0,255), //each color code in rgb in form of array
						array(0,255,255),
						array(255,0,0),
						//    true //uncomment if overwrite existing colors,comment if append new colors to existing ones
				),
				'pathType'=>2 //default:1.The Alternate path type    1 or 2.change pathtype to 2 if 1 does not work.
			)
		);
		$this->Captcha->getCaptcha();
	}



}